package com.xinyue.exceltool.logic;

import com.xinyue.exceltool.config.ExcelToolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;

/**
 * 读取excel的入口类
 *
 * @author wgs
 */
@Service
public class ReadExcelService {

    private Logger logger = LoggerFactory.getLogger(ReadExcelService.class);
    @Autowired
    private ExcelToolConfig excelToolConfig;

    public void printConfig() {
        logger.info("-----配置信息-----\n{}", excelToolConfig.toString());
    }

    public void checkExcelFilePath() {
        if (StringUtils.isEmpty(excelToolConfig.getExcelFilePath())) {
            logger.error("Excel文件所在目录路径不能为空");
        }
    }

    public void readExcel() {
        File excelDir = new File(excelToolConfig.getExcelFilePath());
        if (excelDir.exists()) {
            File[] excelFiles = excelDir.listFiles();
            logger.info("开始读取Excel文件，文件数量：{}", excelFiles.length);
            for (File excelFile : excelFiles) {
                logger.info("开始读取excel文件:{}", excelFile.getAbsolutePath());
                ReadOneExcelManager readOneExcelManager = new ReadOneExcelManager(excelFile, excelToolConfig);
                readOneExcelManager.readExcelData();
            }
            logger.info("----读取所有配置表完成---");
        } else {
            logger.error("Excel文件配置路径不存在：{}", excelToolConfig.getExcelFilePath());
        }
    }

}
